﻿package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;

import server.ResBookInterface;
import server.ResCustomerInterface;
import server.ResWaiterInterface;

import java.awt.Font;
import java.awt.Color;


public class ResBookInfoGUI extends JDialog {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	
	private JComboBox comboBox;
	


	/**
	 * Launch the application.
	
	 public static void main(String[] args){
		 EventQueue.invokeLater(new Runnable(){
			 	 
			public void run() {
				try {
					ResBookInfoGUI frame = new ResBookInfoGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
});
}
 */
	/**
	 * Create the frame.
	 */
	public ResBookInfoGUI() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 316, 455);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("\u9884\u8BA2\u65E5\u671F");
		label.setBackground(new Color(204, 204, 51));
		label.setForeground(new Color(176, 196, 222));
		label.setFont(new Font("方正舒体", Font.PLAIN, 18));
		label.setBounds(27, 19, 81, 20);
		contentPane.add(label);
		
		

		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();
		
		
		String[] Days={"0","0","0","0","0","0","0"};
		
		Days[0]=sdf.format(date);
		
		for(int i=1;i<7;i++)
		{
			date.setTime(date.getTime() + 1* 24 * 60
				* 60 * 1000);

	    	Days[i]=sdf.format(date);
		}
		
		
		
		
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(Days));
		comboBox.setBounds(118, 21, 93, 21);
		contentPane.add(comboBox);
		
		JLabel label_1 = new JLabel("\u4EBA\u6570");
		label_1.setForeground(new Color(0, 0, 0));
		label_1.setFont(new Font("方正舒体", Font.PLAIN, 18));
		label_1.setBounds(37, 58, 54, 15);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setBounds(118, 52, 81, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel label_2 = new JLabel("\u59D3\u540D");
		label_2.setFont(new Font("方正舒体", Font.PLAIN, 18));
		label_2.setBounds(37, 97, 54, 15);
		contentPane.add(label_2);
		
		textField_1 = new JTextField();
		textField_1.setBounds(118, 96, 81, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel label_3 = new JLabel("\u7535\u8BDD");
		label_3.setFont(new Font("方正舒体", Font.PLAIN, 18));
		label_3.setBounds(37, 138, 54, 15);
		contentPane.add(label_3);
		
		textField_2 = new JTextField();
		textField_2.setBounds(117, 138, 133, 18);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		

		
		JButton button = new JButton("\u786E\u5B9A");
		button.setFont(new Font("方正舒体", Font.PLAIN, 18));
		button.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				if(textField.getText().isEmpty()==false&&
						textField_1.getText().isEmpty()==false&&textField_2.getText().isEmpty()==false)
				{
					
					Date day=new Date();
					Configuration config=new Configuration();
					String rmiAddress=config.getRmiAddress();
					
					try {
						ResWaiterInterface waiter = (ResWaiterInterface) Naming.lookup(rmiAddress+"ResWaiter");
						ResCustomerInterface cus=(ResCustomerInterface)Naming.lookup(rmiAddress+"ResCustomer");
						ResBookInterface book=(ResBookInterface)Naming.lookup(rmiAddress+"ResBookInfo");
						
				//		String t1=""+book.addResOrder(workNum, pNum, name, phone, date)
					//	int waiterNum=waiter.searchWaiter();
						
						SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
						String d= (String) comboBox.getSelectedItem();
						Date date=s.parse(d);
						
						
						int t=cus.searchWaiter();
						int a1=waiter.RecordBook(t, new Integer(textField.getText()),textField_1.getText(), textField_2.getText(), date);
						String t1=""+a1;
						cus.leaveWaier(t);
						dispose();
						JOptionPane.showMessageDialog(null, "恭喜您预订成功!!\n您的预订号为"+t1+"\n千万不要忘记!");
	    			    
	    			
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ParseException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
								
     				
		
				}
				else
				{
					JOptionPane.showMessageDialog(null, "您的信息填写有误，请重新填写！");
				}
			}
		});
		button.setBounds(211, 184, 71, 20);
		contentPane.add(button);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon("src/client/images/33515_200904051745311.jpg"));
		lblNewLabel.setBounds(0, 0, 302, 417);
		contentPane.add(lblNewLabel);
	}

}
